//
//  ViewController.m
//  NewBiuWeather
//
//  Created by 楼楚 on 16/4/9.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "ForecastController.h"

#import "GetWeatherData.h"
#import "WeatherData.h"
#import "LocationManager.h"

#import "GCD.h"

#import "WeatherNumberMeaningTransform.h"

@interface ViewController ()<SDWebImageManagerDelegate,UITableViewDelegate,GetWeatherDataDelegate,UITableViewDataSource,UIViewControllerTransitioningDelegate,LocationManagerDelegate,CLLocationManagerDelegate>
{
    CLGeocoder         *_geocoder;
    int a;
}
@property (nonatomic,strong) LocationManager *locationManager;
@property (nonatomic,strong) GetWeatherData  *getWeatherData;
@property (nonatomic,strong) UILabel         *textLabel;
@property (nonatomic,strong) NSString        *cityName;
@property (nonatomic,strong) NSString        *sublocalityName;

@property (nonatomic,strong) NSString        *cityCNName;
@property (nonatomic,strong) UITableView     *tableView;

@property (nonatomic,strong) UIView          *header;
@property (nonatomic,strong) UIView          *footer;

@property (nonatomic,strong) UILabel         *weekDayInfoLabel;
@property (nonatomic,strong) UILabel         *dateLabel;
@property (nonatomic,strong) UILabel         *dayminTempLabel;
@property (nonatomic,strong) UILabel         *daymaxTempLabel;
@property (nonatomic,strong) UILabel         *dayweatherLabel;
@property (nonatomic,strong) UILabel         *dayweatherInfoLabel;

@property (nonatomic,strong) UILabel         *hourTempLabel;
@property (nonatomic,strong) UILabel         *hourTimeLabel;
@property (nonatomic,strong) UILabel         *windDiscrLabel;
@property (nonatomic,strong) UILabel         *humidityLabel;//%

@property (nonatomic,strong) UILabel         *cityLabel;
@property (nonatomic,strong) UILabel         *maxMinLabel;
@property (nonatomic,strong) UILabel         *currentLabel;
@property (nonatomic,strong) UILabel         *weatherIconLabel;
@property (nonatomic,strong) UILabel         *weatherDescriLabel;
@property (nonatomic,strong) UILabel         *windDirLabel;

@property (nonatomic,strong) UIButton        *movingSquare;
@property (nonatomic) CGPoint startPoint;
@property (nonatomic) CGPoint endPoint;

@property (nonatomic,strong) GCDTimer        *timer;
@property (nonatomic,strong) CAShapeLayer    *shapeLayer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.locationManager = [[LocationManager alloc]init];
    self.locationManager.delegate = self;
    [self.locationManager start];
    
    a = 0 ;
    
    [self loadBackgroundImage];

    self.getWeatherData = [GetWeatherData new];
    self.getWeatherData.delegate = self;

//    self.getWeatherData.cityStr = @"city=yuhang";
//    [self.getWeatherData getData];
}

#pragma mark - 定位成功之后获取地名
- (void)mapManager:(LocationManager *)manager didUpdateAndGetLastCLLocation:(CLLocation *)location{
    NSLog(@"定位成功");
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    _geocoder = [[CLGeocoder alloc]init];
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark=[placemarks lastObject];
        
        NSString *str1 = [placemark.addressDictionary objectForKey:@"SubLocality"];
        NSString *str2 = [placemark.addressDictionary objectForKey:@"City"];
        self.cityCNName = str1 ;
        
        NSString *city  = [str1 substringToIndex:([str1  length]-1)];
        NSString *subcity = [str2 substringToIndex:([str2  length]-1)];
        NSString *city1 = [NSString stringWithFormat:@"city=%@",city];
        NSString *city2 = [NSString stringWithFormat:@"city=%@",subcity];
        self.sublocalityName = [city1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        self.cityName = [city2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",self.cityName);
        [self performSelector:@selector(delayRunEvent:)
                   withObject:self.sublocalityName
                   afterDelay:0.3f];

    }];
    
}
- (void)delayRunEvent:(id)object {
    if (a == 0) {
        NSLog(@"3-----3");
        NSLog(@"%@",object);
        self.getWeatherData.cityStr = object;
        
        [self.getWeatherData getData];
        
    }
    else
    {
        NSLog(@"zhix");
    }
    a = a + 1;
}

#pragma mark - 显示具体信息
- (void)showViews{
    [self stopWatingAnimation];
    self.tableView = [[UITableView alloc]init];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor colorWithWhite:1 alpha:0.2];
    self.tableView.pagingEnabled = YES;
    [self.view addSubview:self.tableView];
    
    self.header = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.header.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = self.header;
    
    self.currentLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, Height-180, Width - 40, 110)];
    self.currentLabel.backgroundColor = [UIColor clearColor];
    self.currentLabel.textColor = [UIColor whiteColor];
    self.currentLabel.alpha = 0;
    self.currentLabel.font =[UIFont fontWithName:numberfont size:120];
    
    self.maxMinLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, Height - 70, Width - 40, 40)];
    self.maxMinLabel.backgroundColor = [UIColor clearColor];
    self.maxMinLabel.textColor = [UIColor whiteColor];
    self.maxMinLabel.alpha = 0;
    self.maxMinLabel.textAlignment = NSTextAlignmentLeft;
    self.maxMinLabel.font = [UIFont fontWithName:numberfont size:28];
    
    self.cityLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, Width, 30)];
    self.cityLabel.backgroundColor = [UIColor clearColor];
    self.cityLabel.textColor = [UIColor whiteColor];
    self.cityLabel.alpha = 0;
    self.cityLabel.font = [UIFont systemFontOfSize:18];
    self.cityLabel.textAlignment = NSTextAlignmentCenter;
    
    self.weatherDescriLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, Height - 210, 40, 30)];
    self.weatherDescriLabel.backgroundColor = [UIColor clearColor];
    self.weatherDescriLabel.textColor = [UIColor whiteColor];
    self.weatherDescriLabel.alpha = 0;
    self.weatherDescriLabel.font = [UIFont systemFontOfSize:18];
    self.weatherDescriLabel.textAlignment = NSTextAlignmentRight;
    
    self.weatherIconLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, Height - 210, 60, 30)];
    self.weatherIconLabel.backgroundColor = [UIColor clearColor];
    self.weatherIconLabel.textColor = [UIColor whiteColor];
    self.weatherIconLabel.alpha = 0;
    self.weatherIconLabel.textAlignment = NSTextAlignmentCenter;
    self.weatherIconLabel.font = [UIFont fontWithName:WEATHER_TIME size:28];
    
    [self.header addSubview:self.currentLabel];
    [self.header addSubview:self.maxMinLabel];
    [self.header addSubview:self.cityLabel];
    [self.header addSubview:self.weatherDescriLabel];
    [self.header addSubview:self.weatherIconLabel];
    
    self.cityLabel.text =self.cityCNName;
//    self.cityLabel.text = @"余杭";
    NSLog(@"-----------%@",self.cityLabel.text);
    self.currentLabel.text = [NSString stringWithFormat:@"%@°",self.getWeatherData.weatherData.today.temp];
    self.currentLabel.text = self.getWeatherData.weatherData.today.temp;
    self.maxMinLabel.text = [NSString stringWithFormat:@"%@°/%@°", self.getWeatherData.weatherData.week.maxTmp,self.getWeatherData.weatherData.week.minTmp];
    self.weatherDescriLabel.text = self.getWeatherData.weatherData.today.weatherTxt;
    self.weatherIconLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:self.getWeatherData.weatherData.today.weatherCode];
    
   [ UIView animateWithDuration:1.0 animations:^{
       self.cityLabel.alpha = 1.0;
    } completion:^(BOOL finished) {
        NSLog(@"citylabel");
    }];
    
    [ UIView animateWithDuration:1.5 animations:^{
        self.weatherIconLabel.alpha = 1.0;
        self.weatherDescriLabel.alpha = 1.0;
    } completion:^(BOOL finished) {
        NSLog(@"weather");
    }];
    [ UIView animateWithDuration:1.8 animations:^{
        self.currentLabel.alpha = 1.0;
    } completion:^(BOOL finished) {
        NSLog(@"currenttmp");
    }];
    [ UIView animateWithDuration:2.0 animations:^{
        self.maxMinLabel.alpha = 1.0;
    } completion:^(BOOL finished) {
        NSLog(@"maxmin");
    }];
  
}
#pragma mark - 刷新成功之前隐藏
- (void)hideViews{
   
    self.currentLabel.text = @"";
    self.maxMinLabel.text = @"";
    self.cityLabel.text = @"";
    self.weatherIconLabel.text = @"";
    self.weatherDescriLabel.text = @"";

}

#pragma mark - 上拉转预报
#pragma mark - 请求到天气数据之后再显示画面
- (void)sucess:(BOOL)sucess{
    if (sucess) {
        if (self.getWeatherData.weatherData.today.temp == nil) {
            [self performSelector:@selector(delayRunEvent:)
                       withObject:self.cityName
                       afterDelay:0.3f];

        }else{
            [self showViews];}
    }
}

#pragma mark - 下拉刷新
- (void)pullDownToRefreshData
{
    [self hideViews];
//    [self showWaitingAnimation];
    [self.locationManager start];
    a = 0;
    
}
#pragma mark - 播放等待动画
- (void)showWaitingAnimation{
//    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
//    shapeLayer.frame         = CGRectMake(0, 0, 600, 300);
//    shapeLayer.path          = [self path].CGPath;
//    
//    shapeLayer.fillColor   = [UIColor clearColor].CGColor;
//    shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
//    shapeLayer.lineWidth   = 0.5f;
//    shapeLayer.opacity     = 0.5f;
//    shapeLayer.position    = self.view.middlePoint;
//    shapeLayer.shadowColor = [UIColor redColor].CGColor;
//    [shapeLayer setTransform:CATransform3DMakeScale(0.65, 0.65, 1.f)];
//    
//    [self.view.layer addSublayer:shapeLayer];
//    self.shapeLayer           = [CAShapeLayer layer];
//    self.shapeLayer.frame     = CGRectMake(0, 0, 600, 300);
//    self.shapeLayer.path      = [self path].CGPath;
//    self.shapeLayer.strokeEnd = 0.f;
//    
//    self.shapeLayer.fillColor     = [UIColor clearColor].CGColor;
//    self.shapeLayer.strokeColor   = [UIColor redColor].CGColor;
//    self.shapeLayer.lineWidth     = 2.f;
//    self.shapeLayer.position      = self.view.middlePoint;
//    self.shapeLayer.shadowColor   = [UIColor whiteColor].CGColor;
//    self.shapeLayer.shadowOpacity = 1.f;
//    self.shapeLayer.shadowRadius  = 4.f;
//    [self.shapeLayer setTransform:CATransform3DMakeScale(0.65, 0.65, 1.f)];
//    [self.view.layer addSublayer:self.shapeLayer];
//    
//    CGFloat MAX = 0.98f;
//    CGFloat GAP = 0.02;
//    self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
//    [self.timer event:^{
//        
//        CABasicAnimation *aniStart = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
//        aniStart.fromValue         = [NSNumber numberWithFloat:0.f];
//        aniStart.toValue           = [NSNumber numberWithFloat:MAX];
//        aniStart.duration          = 4.9f;
//        
//        CABasicAnimation *aniEnd   = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
//        aniEnd.fromValue           = [NSNumber numberWithFloat:0.f + GAP];
//        aniEnd.toValue             = [NSNumber numberWithFloat:MAX + GAP];
//        aniEnd.duration            = 4.9f;
//        
//        CAAnimationGroup *group = [CAAnimationGroup animation];
//        group.duration          = 4.9f;
//        group.animations        = @[aniEnd, aniStart];
//        
//        self.shapeLayer.strokeStart   = MAX;
//        self.shapeLayer.strokeEnd     = MAX + GAP;
//        [self.shapeLayer addAnimation:group forKey:nil];
//        
//    } timeIntervalWithSecs:5.f delaySecs:1.f];
//    [self.timer start];
    
//    self.startPoint = CGPointMake(20, 20);
//    self.endPoint = CGPointMake(Width, Height);
//    
//    self.movingSquare = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    self.movingSquare.backgroundColor = [UIColor blackColor];
//    [self.movingSquare setFrame:CGRectMake(0, 0, 10, 10)];
//    self.movingSquare.center = self.startPoint;
//    [self.movingSquare.layer setMasksToBounds:YES];
//    [self.movingSquare.layer setCornerRadius:5.f];
//    [self.view addSubview:self.movingSquare];
//    
//        CAKeyframeAnimation *squareAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//    squareAni.path = [self path].CGPath;
//    squareAni.duration = 1.f;
//    squareAni.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    self.movingSquare.center = self.endPoint;
//    
//    [self.movingSquare.layer addAnimation:squareAni forKey:nil];
    
}
- (UIBezierPath *)path{
    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
//
//    [bezierPath moveToPoint:self.startPoint];
//    [bezierPath addLineToPoint:CGPointMake(0, 20)];
//    [bezierPath addLineToPoint:CGPointMake(20, 20)];
//    [bezierPath addLineToPoint:CGPointMake(20,40)];
//    [bezierPath addLineToPoint:CGPointMake(40, 40)];
//    [bezierPath addLineToPoint:CGPointMake(40, 60)];
//    [bezierPath addLineToPoint:CGPointMake(60, 60)];
//    [bezierPath addLineToPoint:CGPointMake(60, 80)];
//    [bezierPath addLineToPoint:self.endPoint];
////
//    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
//    [bezierPath moveToPoint: CGPointMake(0, 150)];
//    [bezierPath addLineToPoint: CGPointMake(68, 150)];
//    [bezierPath addLineToPoint: CGPointMake(83, 107)];
//    [bezierPath addLineToPoint: CGPointMake(96, 206)];
//    [bezierPath addLineToPoint: CGPointMake(102, 150)];
//    [bezierPath addLineToPoint: CGPointMake(116, 150)];
//    [bezierPath addLineToPoint: CGPointMake(127, 82)];
//    [bezierPath addLineToPoint: CGPointMake(143, 213)];
//    [bezierPath addLineToPoint: CGPointMake(160, 150)];
//    [bezierPath addLineToPoint: CGPointMake(179, 150)];
//    [bezierPath addLineToPoint: CGPointMake(183, 135)];
//    [bezierPath addLineToPoint: CGPointMake(192, 169)];
//    [bezierPath addLineToPoint: CGPointMake(199, 150)];
//    [bezierPath addLineToPoint: CGPointMake(210, 177)];
//    [bezierPath addLineToPoint: CGPointMake(227, 70)];
//    [bezierPath addLineToPoint: CGPointMake(230, 210)];
//    [bezierPath addLineToPoint: CGPointMake(249, 135)];
//    [bezierPath addLineToPoint: CGPointMake(257, 150)];
//    [bezierPath addLineToPoint: CGPointMake(360, 150)];
//    [bezierPath addLineToPoint: CGPointMake(372, 124)];
//    [bezierPath addLineToPoint: CGPointMake(395, 197)];
//    [bezierPath addLineToPoint: CGPointMake(408, 82)];
//    [bezierPath addLineToPoint: CGPointMake(416, 150)];
//    [bezierPath addLineToPoint: CGPointMake(424, 135)];
//    [bezierPath addLineToPoint: CGPointMake(448, 224)];
//    [bezierPath addLineToPoint: CGPointMake(456, 107)];
//    [bezierPath addLineToPoint: CGPointMake(463, 150)];
//    [bezierPath addLineToPoint: CGPointMake(600, 150)];
//    bezierPath.lineCapStyle = kCGLineCapSquare;
//    
//    bezierPath.lineJoinStyle = kCGLineJoinBevel;
//    
//    [UIColor.blackColor setStroke];
//    bezierPath.lineWidth = 1;
//    [bezierPath stroke];
    
    return bezierPath;
    
}
- (void)stopWatingAnimation{
    [self.shapeLayer removeFromSuperlayer];
    [self.timer destroy];
//    [self.movingSquare removeFromSuperview];
}
#pragma mark - 表格
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.getWeatherData.weatherData.hourly.hourlyFor.count;
    }
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    cell.backgroundColor = [UIColor clearColor];
    //    如果如果没有多余单元，则需要创建新的单元
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
        cell.backgroundColor = [UIColor clearColor];
    }
    
    else {
        while ([cell.contentView.subviews lastObject ]!=nil) {
            [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];
        }
    }
    NSUInteger row = [indexPath row];
    
    if (indexPath.section == 1) {
        NSDictionary *todayInfo =self.getWeatherData.weatherData.week.oneWeek[row];
        NSDictionary *dic1 = [todayInfo objectForKey:@"tmp"];
        NSDictionary *dic2 = [todayInfo objectForKey:@"cond"];
        NSString *string = [todayInfo objectForKey:@"date"];
        NSString *newStr = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
        NSDateFormatter *inputFormatter =  [[NSDateFormatter alloc]init];
        [inputFormatter setDateFormat:@"yyyyMMdd"];
        NSDate *inputDate = [inputFormatter dateFromString:newStr];
        NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
        NSInteger frominterval = [fromzone secondsFromGMTForDate: inputDate];
        NSDate *fromDate = [inputDate  dateByAddingTimeInterval: frominterval];
        NSDateFormatter *new = [[NSDateFormatter alloc]init];
        [new setDateFormat:@"EEE"];
        NSString *weekStr = [new stringFromDate:fromDate];
        
        // 星期几
        if (iPhone4_4s || iPhone5_5s) {
            
            self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
            self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
            self.weekDayInfoLabel.font          = [UIFont fontWithName:numberfont size:28.f];
            self.weekDayInfoLabel.textColor = [UIColor whiteColor];
            self.weekDayInfoLabel.text          = weekStr;
            
        } else if (iPhone6_6s) {
            
            self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
            self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
            self.weekDayInfoLabel.font          = [UIFont fontWithName:numberfont size:34.f];
            self.weekDayInfoLabel.textColor = [UIColor whiteColor];
            self.weekDayInfoLabel.text          = weekStr;
            
        } else if (iPhone6_6sPlus) {
            
            self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
            self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
            self.weekDayInfoLabel.font          = [UIFont fontWithName:numberfont size:40.f];
            self.weekDayInfoLabel.textColor = [UIColor whiteColor];
            self.weekDayInfoLabel.text          = weekStr;
            
        } else {
            
            self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
            self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
            self.weekDayInfoLabel.font          = [UIFont fontWithName:numberfont size:28.f];
            self.weekDayInfoLabel.textColor = [UIColor whiteColor];
            self.weekDayInfoLabel.text          = weekStr;
        }
        
        [self.weekDayInfoLabel sizeToFit];
        [cell.contentView addSubview:self.weekDayInfoLabel];
        
        // 日期
        self.dateLabel               = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 12.f)];
        self.dateLabel.textAlignment = NSTextAlignmentCenter;
        self.dateLabel.font          = [UIFont systemFontOfSize:8.f];
        self.dateLabel.text          = @"2015.2.25";
        self.dateLabel.textColor     = [UIColor whiteColor];
        
        UIView *dateBlackView         = [[UIView alloc] initWithFrame:CGRectMake(Width - 50, 0, 50, 12.f)];
        dateBlackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.95];
        [dateBlackView addSubview:self.dateLabel];
        
        // 最低温度
        if (iPhone4_4s || iPhone5_5s) {
            
            self.dayminTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
            self.dayminTempLabel.textAlignment = NSTextAlignmentLeft;
            self.dayminTempLabel.font          = [UIFont fontWithName:numberfont size:28];
            //        self.minTempLabel.text          = @"-200.0°";
            self.dayminTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
        } else if (iPhone6_6s) {
            
            self.dayminTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(110, 25, 200, 70)];
            self.dayminTempLabel.textAlignment = NSTextAlignmentLeft;
            self.dayminTempLabel.font          = [UIFont fontWithName:numberfont size:34];
            self.dayminTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
            
        } else if (iPhone6_6sPlus) {
            
            self.dayminTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(120, 25, 200, 70)];
            self.dayminTempLabel.textAlignment = NSTextAlignmentLeft;
            self.dayminTempLabel.font          = [UIFont fontWithName:numberfont size:40];
            self.dayminTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
        } else {
            
            self.dayminTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
            self.dayminTempLabel.textAlignment = NSTextAlignmentLeft;
            self.dayminTempLabel.font          = [UIFont fontWithName:numberfont size:28];
            self.dayminTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];    }
        self.dayminTempLabel.textColor = [UIColor whiteColor];
        [self.dayminTempLabel sizeToFit];
        [cell.contentView addSubview:self.dayminTempLabel];
        
        
        // 最高温度
        if (iPhone4_4s || iPhone5_5s) {
            
            self.daymaxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 200, 70)];
            self.daymaxTempLabel.textAlignment = NSTextAlignmentLeft;
            self.daymaxTempLabel.font          = [UIFont fontWithName:numberfont size:28];
            //        self.maxTempLabel.text          = @"-200.0°";
            self.daymaxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
        } else if (iPhone6_6s) {
            
            self.daymaxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80 + 20, 25, 200, 70)];
            self.daymaxTempLabel.textAlignment = NSTextAlignmentLeft;
            self.daymaxTempLabel.font          = [UIFont fontWithName:numberfont size:34];
            self.daymaxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
        } else if (iPhone6_6sPlus) {
            
            self.daymaxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80 + 40, 25, 200, 70)];
            self.daymaxTempLabel.textAlignment = NSTextAlignmentLeft;
            self.daymaxTempLabel.font          = [UIFont fontWithName:numberfont size:40];
            self.daymaxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
            
        } else {
            
            self.daymaxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 200, 70)];
            self.daymaxTempLabel.textAlignment = NSTextAlignmentLeft;
            self.daymaxTempLabel.font          = [UIFont fontWithName:numberfont size:28];
            self.daymaxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];    }
        self.daymaxTempLabel.textColor = [UIColor whiteColor];
        [self.daymaxTempLabel sizeToFit];
        [cell.contentView addSubview:self.daymaxTempLabel];
        
        
        // 天气图标
        if (iPhone4_4s || iPhone5_5s) {
            
            self.dayweatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 60)];
            self.dayweatherLabel.textAlignment = NSTextAlignmentCenter;
            self.dayweatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:40.f];
            self.dayweatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
        } else if (iPhone6_6s) {
            
            self.dayweatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250 + 40, 20, 60, 60)];
            self.dayweatherLabel.textAlignment = NSTextAlignmentCenter;
            self.dayweatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:45.f];
            self.dayweatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
            
        } else if (iPhone6_6sPlus) {
            
            self.dayweatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250 + 70, 24, 60, 60)];
            self.dayweatherLabel.textAlignment = NSTextAlignmentCenter;
            self.dayweatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:50.f];
            self.dayweatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
            
        } else {
            
            self.dayweatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 60)];
            self.dayweatherLabel.textAlignment = NSTextAlignmentCenter;
            self.dayweatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:40.f];
            self.dayweatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
        }
        self.dayweatherLabel.textColor = [UIColor whiteColor];
        [cell.contentView addSubview:self.dayweatherLabel];
        
        
        // 天气描述信息
        self.dayweatherInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(5, 15, 300, 12)];
        self.dayweatherInfoLabel.textAlignment = NSTextAlignmentLeft;
        self.dayweatherInfoLabel.font          = [UIFont systemFontOfSize:10];
        self.dayweatherInfoLabel.text = [dic2 objectForKey:@"txt_d"];
        self.dayweatherInfoLabel.textColor = [UIColor whiteColor];
        [cell.contentView addSubview:self.dayweatherInfoLabel];
        // 线条
        UIView *line         = [[UIView alloc] initWithFrame:CGRectMake(0, Width / 4.f - 0.5, Width, 0.5)];
        line.backgroundColor = [UIColor blackColor];
        line.alpha           = 0.1;
        [cell.contentView addSubview:line];
    }else{
 /* 每个三小时预报 */
        NSDictionary *hourlyInfo = self.getWeatherData.weatherData.hourly.hourlyFor[row];
        NSString *timeInfo = [hourlyInfo objectForKey:@"date"];
        NSString *tempInfo = [hourlyInfo objectForKey:@"tmp"];
        NSString *humidityInfo = [hourlyInfo objectForKey:@"hum"];
        NSDictionary *windInfo = [hourlyInfo objectForKey:@"wind"];
        NSString *wind1 = [windInfo objectForKey:@"dir"];
        NSString *wind2 = [windInfo objectForKey:@"sc"];
        NSLog(@"wwwwwwww%@",wind2);
        NSString *hhmmtime = [timeInfo substringFromIndex:11];
        
        self.hourTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
        self.hourTimeLabel.textAlignment = NSTextAlignmentLeft;
        
        
        if (iPhone4_4s || iPhone5_5s) {
            self.hourTimeLabel.font          = [UIFont fontWithName:numberfont size:20.f];
            
        } else if (iPhone6_6s){
            
            self.hourTimeLabel.font          = [UIFont fontWithName:numberfont size:28.f];
        } else if (iPhone6_6sPlus){
            self.hourTimeLabel.font          = [UIFont fontWithName:numberfont size:28.f];
            
        } else {
            self.hourTimeLabel.font          = [UIFont fontWithName:numberfont size:20.f];
        }
        self.hourTimeLabel.text = hhmmtime;//时间
        
        self.hourTimeLabel.textColor = [UIColor whiteColor];
        [self.hourTimeLabel sizeToFit];
        [cell.contentView addSubview:self.hourTimeLabel];
        
        
        if (iPhone4_4s || iPhone5_5s) {
            self.hourTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
            self.hourTempLabel.textAlignment = NSTextAlignmentLeft;
            self.hourTempLabel.font          = [UIFont fontWithName:numberfont size:20];
            
        } else if (iPhone6_6s){
            self.hourTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(110, 25, 200, 70)];
            self.hourTempLabel.textAlignment = NSTextAlignmentLeft;
            self.hourTempLabel.font          = [UIFont fontWithName:numberfont size:28];
            
            
        } else if (iPhone6_6sPlus){
            self.hourTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(120, 25, 200, 70)];
            self.hourTempLabel.textAlignment = NSTextAlignmentLeft;
            self.hourTempLabel.font          = [UIFont fontWithName:numberfont size:28];
            
            
        } else {
            self.hourTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
            self.hourTempLabel.textAlignment = NSTextAlignmentLeft;
            self.hourTempLabel.font          = [UIFont fontWithName:numberfont size:20];
            
        }
        self.hourTempLabel.text = [NSString stringWithFormat:@"%@°",tempInfo];//每3小时温度
        self.hourTempLabel.textColor = [UIColor whiteColor];
        [self.hourTempLabel sizeToFit];
        
        [cell.contentView addSubview:self.hourTempLabel];
        
        
        if (iPhone4_4s || iPhone5_5s) {
            self.windDiscrLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 260, 70)];
            self.windDiscrLabel.textAlignment = NSTextAlignmentLeft;
            self.windDiscrLabel.font          = [UIFont fontWithName:numberfont size:18];
            
        } else if (iPhone6_6s){
            self.windDiscrLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80+ 20, 25, 260, 70)];
            self.windDiscrLabel.textAlignment = NSTextAlignmentLeft;
            self.windDiscrLabel.font          = [UIFont fontWithName:numberfont size:28];
            
        } else if (iPhone6_6sPlus){
            self.windDiscrLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80+ 40, 25, 260, 70)];
            self.windDiscrLabel.textAlignment = NSTextAlignmentLeft;
            self.windDiscrLabel.font          = [UIFont fontWithName:numberfont size:28];
            
        } else {
            self.windDiscrLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 260, 70)];
            self.windDiscrLabel.textAlignment = NSTextAlignmentLeft;
            self.windDiscrLabel.font          = [UIFont fontWithName:numberfont size:18];
            
        }
        self.windDiscrLabel.text = [NSString stringWithFormat:@"%@:%@",wind1,wind2];//天气描述
        self.windDiscrLabel.textColor = [UIColor whiteColor];
        [self.windDiscrLabel sizeToFit];
        [cell.contentView addSubview:self.windDiscrLabel];
        
        
        
        
        UIView *line1        = [[UIView alloc] initWithFrame:CGRectMake(0, Width / 4.f - 0.5, Width, 0.5)];
        line1.backgroundColor = [UIColor blackColor];
        line1.alpha           = 0.05;
        [cell.contentView addSubview:line1];
    }
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return Width/4.f;
}

#pragma mark - 刷新时的飞机动画

- (void)loadBackgroundImage{
    NSError *err;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"]];
    NSData *reponse = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *picDic = [NSJSONSerialization JSONObjectWithData:reponse options:NSJSONReadingMutableContainers error:&err];
    
    NSDictionary *picc = [picDic objectForKey:@"images"];
    NSArray *arr = picc;
    NSArray *arr1 = arr[0];
    NSDictionary *dic = arr1;
    NSString *picStr = [dic objectForKey:@"url"];
    NSLog(@"picstr----%@",picStr);
    NSURL *picURL = [NSURL URLWithString:picStr];
    UIImageView *picView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    picView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.1];
    picView.contentMode = UIViewContentModeScaleAspectFill;
    //    NSData *data = [NSData dataWithContentsOfURL:picURL];
    //    picView.image = [UIImage imageWithData:data];
    [picView sd_setImageWithURL:picURL];
    //    NSLog(@"%f-------%f",picView.image.size.width,picView.image.size.height);
    
    [self.view addSubview:picView];
    
}
- (void)viewWillLayoutSubviews{
//   TODO viewWillLayoutSubviews干嘛用的
    [super viewWillLayoutSubviews];
    
    self.tableView.frame = self.view.bounds;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

    
    if (scrollView.contentOffset.y <= -60.f) {
        [self pullDownToRefreshData];
        }
        
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
